// pages/applycrm/approval/approval.js
const app = getApp()
var CONFIG = require("../../../config/config.js");
var CONFIG = require("../../../utils/share.js");

Page({

    /**
     * 页面的初始数据
     */
    data: {
        index: 0,
        page:0,
        phone:'',
        status:3,  //3 待审核   2：已审核
        list:[],
        keykown:'',
        errcode:'',
        msg:'',
    },
    

    // 切换title
    titleclick(e) {
        var that = this;
        var index = e.target.dataset.index;
        that.setData({
            index: index,
            msg:'',
        })
        wx.showLoading({
            title: '加载中~',
        })
        if (index == 1 || index == '1'){
            that.setData({
                keykown: '',
                list: [],
                page: 0,
                status: 2
            })
        }else{
            that.setData({
                keykown: '',
                list: [],
                page: 0,
                status: 3
            })
        }
        that.auditList();
    },
    

    // 点击查询
    searchclick(e){
        var that = this;
        var e1 = e.detail.value;
        var keykown = e1.keykown;
        if (keykown==''){
            wx.showToast({
                title: '请输入客户姓名或者电话',
                icon: 'none'
            })
        }else{
            wx.showLoading({
                title: '加载中~',
            })
            that.setData({
                keykown: keykown,
                list:[],
                page:0
            })
            wx.setStorageSync('keyapprolal', keykown);
            that.auditList();
        }
    },

    
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        var phone = wx.getStorageSync('tel');
        that.setData({
            phone:phone
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        var phone = wx.getStorageSync('tel');
        if (wx.getStorageSync('keyapprolal')){
            that.setData({
                keykown: wx.getStorageSync('keyapprolal')
            })
        }
        that.setData({
            list: [],
            phone: phone
        })
        that.auditList();
    },

    auditList(){
        var that = this;
        var phone = that.data.phone;
        var page = that.data.page;
        var status = that.data.status;
        var keykown = that.data.keykown;
        wx.request({
            url: CONFIG.API_HOST + 'crm/Roleauth/auditList',
            data: {
                phone: phone,  //
                page: page,
                status: status,
                keyword: keykown
            },
            method: 'GET',
            success: function (res) {
                wx.hideLoading();
                // console.log(res);
                
                if (res.data.status == 1001 || res.data.status == '1001'){
                    var data = res.data.data;
                    var list = that.data.list;
                    that.setData({
                        list:list.concat(data)
                    })
                    if(page==0||page=='0'){
                        if (data.length == 0 || data.length == '0'){
                            that.setData({
                                msg: '暂无数据',
                            })
                        }
                    }
                }else{
                    that.setData({
                        errcode: res.data.status,
                        msg: res.data.msg,
                    })
                }
            }
        })
    },



    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        wx.removeStorageSync('keyapprolal')
        that.setData({
            list:[],
            page:0,
            keykown:'',
        })
        that.auditList();
        wx.stopPullDownRefresh();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        wx.showLoading({
            title: '加载中~',
        })
        var that = this;
        var page = that.data.page;
        page++
        that.setData({
            page:page
        })
        that.auditList();
    },
})